using In66.Net.EventBus.Cap;
using In66.Net.EventBus.RabbitMq.IIntegration;
using In66.Net.Share.Controller;
using Test.Api.Models.EventModel;

namespace Test.Api.Controllers.V1;

/// <summary>
/// 消息总线测试控制器
/// </summary>
/// <summary>
/// 主控制器
/// </summary>
[ApiVersion("1.0")]
[AllowAnonymous]
[Route("test/[controller]/[action]")]
public class EventBusController : BaseController
{
    private readonly IIntegrationEventBus _ibus;
    private readonly IEventPublisher _eventPublisher;
    public EventBusController(
        IIntegrationEventBus ibus,
        IEventPublisher eventPublisher

        )
    {
        this._ibus = ibus;
        _eventPublisher = eventPublisher;
    }


    /// <summary>
    /// Cap
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<IActionResult> PublishCap()
    {
        var eventmodel = new TestCapEventModel() { CustomerId = "21", TransactionLogId = "222", Amount = "22" };

        await _eventPublisher.PublishAsync(new TestCaptEvent(nameof(TestCaptEvent), eventmodel));

        return Ok();
    }

    /// <summary>
    /// 发送HelloWorld消息
    /// </summary>
    [HttpPost]
    public void HelloWorld()
    {
        var rand = new Random();
        _ibus.Publish(new HelloWorldEvent(), priority: (byte)rand.Next(0, 9));
    }

    /// <summary>
    /// 发送WorkQueues消息
    /// </summary>
    [HttpPost]
    public void WorkQueues()
    {

        for (int i = 0; i < 10; i++)
        {
            _ibus.Publish(new WorkQueuesEvent
            {
                Summary = $"WorkQueuesEvent:{i}"
            });
        }

    }

    /// <summary>
    /// Fanout(发布订阅)发送消息,设置两个队列,所以应该输出两条信息
    /// </summary>
    [HttpPost]
    public void Fanout(CancellationToken cancellationToken)
    {
        _ibus.Publish(new FanoutEventOne(), cancellationToken: cancellationToken);
    }

    /// <summary>
    /// 路由模式(direct)模式发送消息,只向单一主题发送消息
    /// </summary>
    [HttpPost]
    public Task DirectQueue1(CancellationToken cancellationToken)
    {
        Task.Run(() => _ibus.Publish(new DirectEventOne(), "direct.queue1", cancellationToken: cancellationToken), cancellationToken);
        return Task.CompletedTask;
    }

    /// <summary>
    /// 路由模式(direct)发送消息,只向单一主题发送消息
    /// </summary>
    [HttpPost]
    public void DirectQueue2()
    {
        _ibus.Publish(new DirectEventOne(), "direct.queue2");
    }

    /// <summary>
    /// Topic(主题模式)发送消息,向订阅了,[topic.queue.1]主题的队列发送消息.
    /// 只配置了topic.queue.*和topic.queue.1,所以该接口应该只输出两条信息.
    /// </summary>
    [HttpPost]
    public void TopicTo1()
    {
        _ibus.Publish(new TopicEventOne(), "topic.queue.1");
    }

    /// <summary>
    /// Topic(主题模式)发送消息,向订阅了,[topic.queue.2]主题的队列发送消息.
    /// 只配置了topic.queue.*和topic.queue.1,所以该接口应该只输出一条信息.
    /// </summary>
    [HttpPost]
    public void TopicTo2()
    {
        _ibus.Publish(new TopicEventOne(), "topic.queue.2");
    }

    /// <summary>
    /// Topic(主题模式)发送消息,向订阅了,[topic.queue.3]主题的队列发送消息.
    /// 只配置了topic.queue.*和topic.queue.1,所以该接口应该只输出一条信息.
    /// </summary>
    [HttpPost]
    public void TopicTo3()
    {
        _ibus.Publish(new TopicEventOne(), "topic.queue.3");
    }
}